package wsz.email.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import wsz.email.service.MailService;

import javax.mail.MessagingException;

/**
 * 执行邮件发送
 * @author wsz
 * @date 2021/12/28 19:12
 **/
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Override
    public void sendSimpleTextMail(String subject, String content, String[] toWho) throws MessagingException {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //设置邮件的基本信息
        handleBasicInfo(simpleMailMessage,subject,content,toWho);
        //发送邮件
        mailSender.send(simpleMailMessage);
    }

    /**
     * 通用配置信息
     * @param simpleMailMessage
     * @param subject
     * @param content
     * @param toWho
     */
    private void handleBasicInfo(SimpleMailMessage simpleMailMessage, String subject, String content, String[] toWho) throws MessagingException {
        simpleMailMessage.setFrom(mailSender.getUsername());
        //设置邮件的主题
        simpleMailMessage.setSubject(subject);
        //设置邮件的内容
        simpleMailMessage.setText(content);
        //设置邮件的收件人
        simpleMailMessage.setTo(toWho);
    }


}
